<?php
require_once 'db_utility.php';
require_once 'visibility.php';

function print_stories_in_order($result, $show_number){
	echo "<div id=\"storylist\">\n";
	while ($row = $result->fetch_assoc()) {
		print_one_story($row, $show_number);
	}
	echo "</div>\n";
}

function print_stories_in_random($result, $show_number){
	$arr = array();
	while ($row = $result->fetch_assoc()) {
		array_push($arr, $row);
	}
	shuffle($arr);
	echo "<div id=\"storylist\">\n";
	foreach ($arr as $row){
		print_one_story($row, $show_number);
	}
	echo "</div>\n";
}

function get_domain($url)
{
	//$nowww = ereg_replace('www\.','',$url);
	$domain = parse_url($url);
	if(!empty($domain["host"]))
	{
		return $domain["host"];
	} 
	else
	{
		return $domain["path"];
	}
}

function print_one_story($row, $show_number){
	$popularity = $row['popularity'];
	$title = $row['title'];
	$url = $row['url'];
	$source = $row['source'];
	$summary = $row['summary'];
	$story_id = $row['story_id'];

	if(strcmp($source, "text") != 0){
	    $url = "sciencedaily/$source";
	}
	
	//  obscure url
	//	$domain = get_domain($url);
	//	$ip = gethostbyname($domain);
	//	$url = str_replace($domain, $ip, $url);
	
	echo "<div id = \"$story_id\" class = \"storyitem\">\n";
	echo "<div class=\"recbutton\">\n";

	if(!empty($_SESSION["stories"]['onButton']["'$story_id'"])) echo "  <img src=\"images/thumb2.png\" />\n";
	else echo "  <img src=\"images/thumb.png\" />\n";

	if(!$show_number) {
		$popularity=-1;
		echo "  <p class=\"rank\" style=\"opacity:0;\">$popularity</p>\n";
	}
	else{
		echo "  <p class=\"rank\">$popularity</p>\n";
	}
	echo "  <p class=\"recommend\">recommend</p>\n";
	echo "</div>\n";

	echo "<div class = \"content\">\n";
	echo "  <p class=\"title\"><a href=\"$url\">$title</a></p>\n";
	if(strlen($summary) > 300){
		$summary = ">".substr($summary, 0, 300)."...";
	}
	else if(strlen($summary) < 200){
		$summary = "style=\"height:50px;\">".$summary;
	}
	else
	$summary = ">".$summary;
	echo "  <p class=\"summary\" $summary</p>\n";
	echo "</div>\n";
	echo "</div>\n";
}

function print_all_stories(){
    if(!isset($_SESSION['user']['u_visibility'])){
        return;
    }
    $type = $_SESSION['user']['u_visibility'];
    $story_table_name = get_story_table_name($type);
    $story_order_name = get_story_order_name($type);
    $is_show_number = is_show_number($type);
    $conn = db_connect();
    if(strcmp($story_order_name, "random") == 0) {
        // SQL statement for data query
        $strsql="select * from $story_table_name";
        $result = $conn -> query($strsql);
        print_stories_in_random($result, $is_show_number);
        $result -> free();
    }
    else {
        // SQL statement for data query
        $strsql="select * from $story_table_name $story_order_name";
        $result = $conn -> query($strsql);
        print_stories_in_order($result, $is_show_number);
        $result -> free();
    }
    $conn -> close();
}
?>